package example.database;

import static de.paulzerr.Constants.COLUMN_ID;
import static de.paulzerr.Constants.COLUMN_WORD;
import static de.paulzerr.Constants.TABLE_NAME;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBConnector {
	
	private SQLiteOpenHelper db;
	private Activity activity;
	
	public DBConnector(SQLiteOpenHelper sqlOpenHelper, Activity activity){
		db = sqlOpenHelper;
		this.activity = activity;
	}
	
	public void addWordIntoDatabase() {
		SQLiteDatabase database = db.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(COLUMN_WORD, "push up");
		database.insertOrThrow(TABLE_NAME, null, values);	}
	
	
	 public Cursor getWordsFromDatabase() {
	    	String[] from = { COLUMN_ID, COLUMN_WORD};
			SQLiteDatabase database = db.getReadableDatabase();
			Cursor cursor = database.query(TABLE_NAME, from , null, null, null, null, null);
			activity.startManagingCursor(cursor);
			return cursor;
		}
	    
	    public StringBuilder getWordsFromDatabase(Cursor cursor) {
			StringBuilder sb = new StringBuilder();
			while(cursor.moveToNext()){
				long id = cursor.getLong(0);
				String word = cursor.getString(1);
				sb.append(" id  : " + id);
				sb.append(" word: "+ word).append("\n");
			}
			return sb;			
		}
}
